Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
io.sockets.adapter.rooms[roomId] returns undefined after using socket.join[roomId]?

I've been trying to create a sort of multiplayer game where rooms are made by users in socket.io, but joining a user to a room has been difficult, and I recently realized it seems that the rooms aren't actually being created? I know for a fact the "handleNewRoom" function is being run as it does generate an ID and send it to the frontend, so I know "socket.join(roomId);" is being run as well, but even in the same function "io.sockets.adapter.rooms[roomId];" returns undefined (this line is purely there for testing, I meant to only call it in the handleJoinRoom function to see if the room exists at all but I moved it here so it'd be faster to check the existence)

I've been messing with socket.io for a little bit on this project so there might be something very simple that I'm missing, so here's my full server.js file. As far as I've read, the socket.join[roomId] command should create a room regardless of its existence, and then the io.sockets.adapter.rooms[roomId] should return a Map of the specified room, but for some reason it doesn't. Any ideas?

const express = require("express");
const app = express();
const path = require("path");
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

// pull the function that randomly generates an ID
const { makeid } = require("./utils");

// set a variable with all the rooms
const socketRooms = {};

// set the path for the frontend
app.use(express.static(path.join(__dirname, "../front")));

const PORT = process.env.PORT || 3000;

// start the server
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));

// on a new connection to the main server
io.on("connection", (socket) => {
  console.log("User has connected");
  socket.on("newRoom", handleNewRoom);
  socket.on("joinRoom", handleJoinRoom);
  let users = [];

  function handleNewRoom() {
    let roomId = makeid(5);
    socket.join(roomId);
    socketRooms[socket.id] = roomId;
    socket.emit("roomId", roomId);
    socket.number = 1;
    socket.emit("init", 1);
    const room = io.sockets.adapter.rooms[roomId];
    if (room) {
      console.log("I definitely exist");
    }
  }

  function handleJoinRoom(roomId) {
    const room = io.sockets.adapter.rooms[roomId];
    let allUsers;
    if (room) {
      allUsers = room.sockets;
      console.log("I'm here");
    }
    console.log(allUsers);
    console.log(roomId);
    let numSockets = 0;
    if (allUsers) {
      numSockets = Object.keys(allUsers).length;
    }
    if (numSockets === 0) {
      socket.emit("unknownRoom");
      return;
    }
    socketRooms[socket.id] = roomId;
    socket.join(roomId);
    socket.number = 2;
    socket.emit("init", 2);
    socket.emit("roomId", roomId);
  }
});

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!